package cn.myfirefly.moming.portal.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.RoleDataAuthDao;
import cn.myfirefly.moming.common.dao.UserRoleDao;
import cn.myfirefly.moming.common.entity.MmRoleDataAuth;
import cn.myfirefly.moming.common.model.RoleDataAuthDto;
import cn.myfirefly.moming.common.model.RoleDto;
import cn.myfirefly.moming.portal.dao.FunctionActionDao;
import cn.myfirefly.moming.portal.dao.RoleActionDao;
import cn.myfirefly.moming.portal.dao.RoleDao;
import cn.myfirefly.moming.portal.dao.RoleFunctionDao;
import cn.myfirefly.moming.portal.dao.RoleSysDao;
import cn.myfirefly.moming.portal.entity.MMRole;
import cn.myfirefly.moming.portal.entity.MMRoleAction;
import cn.myfirefly.moming.portal.entity.MMRoleFunction;
import cn.myfirefly.moming.portal.entity.MMRoleSys;
import cn.myfirefly.moming.portal.model.FunctionPermissionDto;
import cn.myfirefly.moming.portal.model.RoleActionDto;
import cn.myfirefly.moming.portal.model.RoleFilterDto;
import cn.myfirefly.moming.portal.model.RoleManageSearchDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.CollectionUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class RoleService {
//	private Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private RoleDao roleDao;
	
	@Autowired
	private RoleActionDao roleActionDao;
	
	@Autowired
	private RoleFunctionDao roleFunctionDao;
	
	@Autowired
	private FunctionActionDao functionActionDao;
	
	@Autowired
	private UserRoleDao userRoleDao;
	
	@Autowired
	private RoleSysDao roleSysDao;
	
	@Autowired
	private RoleDataAuthDao roleDataAuthDao;
	
	@SuppressWarnings("rawtypes")
	public PageResultDto getList(RoleFilterDto dto) {
		return roleDao.getList(dto);
	}
	
	public PageResultDto<MMRole> getList(RoleDto dto) {
		
		return roleDao.getList(dto);
	}
	
	public String save(RoleDto dto) {
		MMRole role = null;
		// 更新
		if (StrUtil.notBlank(dto.getRoleSn())) {
			role = roleDao.get(dto.getRoleSn());
			if (role != null && role.getRoleSn() != dto.getRoleSn()) {
				return "角色已存在。";
			}
		} else {
		// 新增
			role = new MMRole();
			role.setCreateBy(dto.getModifyBy());
			role.setCreateDate(dto.getModifyDate());
		}
		BeanUtil.copyBeans(dto, role);
		roleDao.save(role);
		return Const.ResultCode.SUCCESS;
	}
	
	public String savePermission(Integer roleSn, Integer funSn, String funPermission, List<RoleActionDto> list) {
		// 更新角色功能关联表
		MMRoleFunction roleFun = roleFunctionDao.get(roleSn, funSn);
		if (roleFun == null) {
			roleFun = new MMRoleFunction();
		}
		roleFun.setFunSn(funSn);
		roleFun.setRoleSn(roleSn);
		roleFun.setPermission(funPermission);
		roleFunctionDao.save(roleFun);
		
		// 更新角色动作关联表
		roleActionDao.del(roleSn, funSn);
		if (list != null) {
			MMRoleAction roleAction = null;
			for (RoleActionDto roleActionDto : list) {
				roleAction = new MMRoleAction(roleSn, 
						roleActionDto.getFunActionSn(), roleActionDto.getFunSn());
				roleActionDao.save(roleAction);
			}
		}
		
		return Const.ResultCode.SUCCESS;
	}
	
	public String saveDataPermission(Integer roleSn, Integer funSn, Integer dataAuthType, Integer dataAuthObject) {
		if (dataAuthType != null && dataAuthObject != null) {
			MmRoleDataAuth roleDataAuth = new MmRoleDataAuth();
			roleDataAuth.setRoleSn(roleSn);
			roleDataAuth.setFunSn(funSn);
			List<MmRoleDataAuth> list = roleDataAuthDao.findByExample(roleDataAuth);
			if (CollectionUtil.isNotEmpty(list)) {
				roleDataAuth = list.get(0);
			}
			roleDataAuth.setDataAuthObjectId(dataAuthObject);
			roleDataAuth.setDataAuthTypeId(dataAuthType);
			
			roleDataAuthDao.save(roleDataAuth);
		} else if (dataAuthType == null && dataAuthObject == null) {
			MmRoleDataAuth roleDataAuth = new MmRoleDataAuth();
			roleDataAuth.setRoleSn(roleSn);
			roleDataAuth.setFunSn(funSn);
			List<MmRoleDataAuth> list = roleDataAuthDao.findByExample(roleDataAuth);
			if (CollectionUtil.isNotEmpty(list)) {
				roleDataAuth = list.get(0);
			}
			roleDataAuthDao.del(roleDataAuth);
		} else {
			return "数据权限类型和对象，不能只选择一个";
		}
		return Const.ResultCode.SUCCESS;
	}
	
	public RoleDto getRole(Integer roleSn) {
		MMRole role = roleDao.get(roleSn);
		return BeanUtil.copyBeans(RoleDto.class, role);
	}
	
	public List<RoleDto> getRoles(Integer userSn) {
		return userRoleDao.getRoles(userSn);
	}
	
	public String del(Integer roleSn) {
		MMRole role = roleDao.get(roleSn);
		if (role == null) {
			return "角色信息不存在，请刷新页面后重试。";
		}
		if (role != null && role.getRoleSn() != roleSn) {
			return "角色信息已变更，请刷新页面后重试。";
		}
		roleDao.del(role);
		return Const.ResultCode.SUCCESS;
	}
	
	public FunctionPermissionDto getFunctionPermission(String funId, Integer roleSn) {
		return functionActionDao.getFunctionPermission(funId, roleSn);
	}
	
	public RoleDataAuthDto getDataPermission(String funId, Integer roleSn) {
		RoleDataAuthDto dto = roleDataAuthDao.getRoleDataAuth(funId, roleSn);
		return dto;
	}
	
	public List<RoleManageSearchDto> getRolesByLoginName(String loginName) {
		return roleDao.getRolesByLoginName(loginName);
	}
	
	public String saveRoleSys(Integer roleSn, Integer systemId) {
		// 判断当前角色，有没有系统模块下的功能权限
		long count = roleSysDao.countRoleFun(roleSn, systemId);
		if (count > 0) {
			// 说明有系统模块权限，则取得角色和系统模块对应关系
			MMRoleSys roleSys = roleSysDao.get(roleSn, systemId);
			if (roleSys == null) {
				// 如果没有对应关系，则新增
				roleSys = new MMRoleSys();
				roleSys.setRoleId(roleSn);
				roleSys.setSysId(systemId);
				roleSysDao.save(roleSys);
			}
		} else {
			// 说明没有系统模块权限，则取得角色和系统模块对应关系
			MMRoleSys roleSys = roleSysDao.get(roleSn, systemId);
			if (roleSys != null) {
				// 如果有对应关系，则删除
				roleSysDao.del(roleSys);
			}
		}
		
		return Const.ResultCode.SUCCESS;
	}
}
